package com.jaydenxiao.androidfire.ui.main.fragment;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.jaydenxiao.androidfire.R;
import com.jaydenxiao.androidfire.bean.News;
import com.jaydenxiao.androidfire.ui.main.activity.NewSdetailActivity;

import java.util.List;


public class News2Adapter extends RecyclerView.Adapter<News2Adapter.ViewHolder> {

    private List<News> newsList;

    public News2Adapter(List<News> newsList) {
        this.newsList = newsList;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.news_item,null);
        ViewHolder viewHolder = new ViewHolder(view);
        System.out.println("News2Adapter --> onCreateViewHolder");
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, final int i) {
        News news = newsList.get(i);
        Glide.with(viewHolder.newsImage.getContext()).load(news.getPic())
                .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                .placeholder(com.jaydenxiao.common.R.drawable.ic_image_loading)
                .error(com.jaydenxiao.common.R.drawable.ic_empty_picture)
                .centerCrop().override(1090, 1090*3/4)
                .crossFade().into(viewHolder.newsImage);
        viewHolder.newsTitle.setText(news.getTitle());

        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent  intent  = new Intent(v.getContext(), NewSdetailActivity.class); intent.setFlags(i);
                v.getContext().startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return newsList.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        ImageView newsImage;
        TextView newsTitle;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);

            newsImage =(ImageView) itemView.findViewById(R.id.newsPic);
            newsTitle =(TextView) itemView.findViewById(R.id.newsTitle);
        }
    }
}
